/*
 * Created on 13-apr-2005
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */

package it.unibo.deis.lia.location;

public class LayeredSymbolicLocation implements java.io.Serializable{
	
	private static final long serialVersionUID = 1L;

	private String[] locationAddress;
	
	public LayeredSymbolicLocation(String[] locationAddress){
		// locationComponent[0]==country
		// locationComponent[1]==city
		// locationComponent[2...n]==application dependent
		this.locationAddress=locationAddress;
	}
	
	public String getLocationAtLevel(int level){
		return locationAddress[level];
	}
	
	public int getMaxGranularity(){
		return locationAddress.length-1;
	}
	
	public LayeredSymbolicLocation reduceGranularity(int level){
		//System.out.println("Reducing "+this+" to "+level);
		if(locationAddress.length>level){
			String[]newLocationAddress=new String[level+1];
			for(int i=0;i<=level;i++){
				newLocationAddress[i]=locationAddress[i];
			}
			return new LayeredSymbolicLocation(newLocationAddress);
		}
		else{
			return new LayeredSymbolicLocation(locationAddress);
		}
	}
	
	public String toString(){
		String ret="(";
		for(int i=0;i<locationAddress.length-1;i++){
			ret+=locationAddress[i]+",";
		}
		return ret+locationAddress[locationAddress.length-1]+")";
		/*String ret="[ ";
		for(int i=0;i<locationAddress.length-1;i++){
			ret+="("+locationAddress[i]+","+i+") ; ";
		}
		return ret+"("+locationAddress[locationAddress.length-1]+","+(locationAddress.length-1)+") ]";*/
	}
	
	public boolean isContained(LayeredSymbolicLocation loc){
		// la locazione di questo oggetto � contenuto in loc?
		// 1) granularit� di questo oggetto deve essere maggiore o uguale di loc
		//System.out.println("LocationAddress.isContained "+this.getMaxGranularity()+" "+loc.getMaxGranularity());
		//if(this.getMaxGranularity()<loc.getMaxGranularity())return false;
		// 2) tutte le locazioni dei due oggetti devono corrispondere
		for (int i=0;i<=loc.getMaxGranularity()&&i<=this.getMaxGranularity();i++){
			//System.out.println("LocationAddress.isContained "+this.getLocationAtLevel(i)+" "+loc.getLocationAtLevel(i));
			if(!this.getLocationAtLevel(i).equals(loc.getLocationAtLevel(i)))return false;
		}
		return true;
	}
}
